<?php

/**
 * FreeY_Db_Table
 *
 * @auther		FreeYadmin FreeYadmin@gmail.com
 * @update		FreeYadmin 
 * @package		FreeY 
 */	
class FreeY_Table_Update extends FreeY_Table_Abstract
{
	/**
	 * _method_info
	 *
	 * @param $var
	 * @return viod
	 */
	public function start($input)
	{
		$this->input = $input;
	}
	/**
	 * _method_info
	 *
	 * @param $var
	 * @return viod
	 */
	public function run()
	{
		$tablename = $this->input['tablename'];
		$data = $this->input['data'];
		$sql = $this->_getSql($tablename, $data, $this->getWhere());
		return $sql;
	}
	/**
	 * _method_info
	 *
	 * @param $var
	 * @return viod
	 */
	protected function _getSql($tablename, $data, $where)
	{
		if(!is_array($data))
		{
			return '';
		}
		foreach($data as $key=>$value)
		{
			if(! is_numeric($value))
			{
				$v[] = $key . "='" . $value . "'";
			}
			else 
			{
				$v[] = $key . '=' . $value;
			}
		}		
		$vstr=join(',',$v);
		$sql='update '.$tablename.' set '.$vstr . $where;
		return $sql;
	}
	/**
	 * getWhere
	 *
	 * @param $var
	 * @return viod
	 */
	protected function getWhere()
	{
		$arr = $this->input['where'];
		if(! is_array ($arr)) return ;
		$ret=' where 1 ';
		foreach ( $arr as $value )
		{
			$ret .= ' and ' . $value . ' ';
		}
		return $ret;
	}
}
//file end--------------------------------------------------------->